package com.flutter.teach.flutter_view

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.PluginRegistry

/**
 * 描述:
 * 作者:liuhaichao
 * 创建日期：2023/4/11 on 3:05 PM
 */
class MyAndroidPlugin : FlutterPlugin {

    override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        //messenger：用于消息传递，后面介绍 Flutter 与 原生通信时会用到此参数。
        val messenger: BinaryMessenger = binding.binaryMessenger
        //viewTypeId：意思是传递这个viewTypeId会使用MyAndroidViewFactory这个PlatformViewFactory
        binding.platformViewRegistry.registerViewFactory(
            viewTypeId,
            MyAndroidViewFactory(messenger)
        )
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    }

    companion object {
        private const val viewTypeId = "pluginView"

//        @JvmStatic
//        fun registerWith(registrar: PluginRegistry.Registrar) {
//            registrar.platformViewRegistry().registerViewFactory(
//                viewTypeId,
//                MyAndroidViewFactory(registrar.messenger())
//            )
//        }
    }
}